//
//  UPAuthCompletedViewController.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/25.
//

#import "UPAuthCompletedViewController.h"

@interface UPAuthCompletedViewController ()

@property (nonatomic, strong) UIImageView *containerImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *continueButton;
@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) UILabel *nameTitleLabel;
@property (nonatomic, strong) UILabel *nameTextField;
@property (nonatomic, strong) UILabel *idTitleLabel;
@property (nonatomic, strong) UILabel *idTextField;
@property (nonatomic, strong) UILabel *birthdayLabel;
@property (nonatomic, strong) UILabel *birthdayTextField;

@property (nonatomic, strong) UIView *separator1;
@property (nonatomic, strong) UIView *separator2;
@property (nonatomic, strong) UIView *separator3;

@end

@implementation UPAuthCompletedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.bgImageView.image = [UIImage imageNamed:@"auth_completed_bg"];
    self.title = @"";
    self.collectionView.hidden = YES;
    [self commonInitSubviews];
    [self fetchAuthInfo];
}

- (void)fetchAuthInfo {
    [SVProgressHUD show];
    __block typeof(self) weakSelf = self;
    [UPStocksAPIManager fetchAuthenticationWithProductId:self.currentOmniosity.degreeaneous completion:^(IdentityVerificationModel * _Nonnull model, NSString * _Nonnull msg) {
        if (model) {
            [weakSelf updateSubviewsWithModel:model];
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showInfoWithStatus:msg];
        }
    }];
}

- (void)updateSubviewsWithModel:(IdentityVerificationModel *)model {
    self.nameTextField.text = model.cilistyleproof.likeence.quindecimlocalety;
    self.idTextField.text = model.cilistyleproof.likeence.assumeization;
    self.birthdayTextField.text = model.cilistyleproof.likeence.movieion;
}

- (void)commonInitSubviews {
    
    UIImage *bgImage = [UIImage imageNamed:@"auth_cer_completed_bg"];
    CGFloat bgAspectRatio = bgImage.size.width / bgImage.size.height;
    CGFloat bgWidth = kScreenWidth - 2 * 16;
    CGFloat bgHeight = bgWidth / bgAspectRatio;
    [self.containerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view).offset(30);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(bgHeight);
        make.width.mas_equalTo(bgWidth);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.containerImageView);
        make.top.equalTo(self.containerImageView).offset(18);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(50);
        make.centerX.equalTo(self.containerImageView);
        make.height.mas_equalTo(58);
    }];
    
    [self setupContentViews];
    
    UIImage *buttonImage = [UIImage imageNamed:@"button_bg_style5"];
    CGFloat buttonHeight = 50.0;
    CGFloat buttonAspectRatio = buttonImage.size.width / buttonImage.size.height;
    CGFloat buttonWidth = buttonHeight * buttonAspectRatio;
    [self.continueButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(- bottomsafeArea());
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(buttonHeight);
        make.width.mas_equalTo(buttonWidth);
    }];
}

- (void)setupContentViews {
    
    [self.containerImageView addSubview:self.nameTitleLabel];
    [self.containerImageView addSubview:self.nameTextField];

    [self.containerImageView addSubview:self.separator1];
    
    [self.containerImageView addSubview:self.idTitleLabel];
    [self.containerImageView addSubview:self.idTextField];
    [self.containerImageView addSubview:self.separator2];
    
    [self.containerImageView addSubview:self.birthdayLabel];
    [self.containerImageView addSubview:self.birthdayTextField];
    
    [self.containerImageView addSubview:self.separator3];
    
    CGFloat labelHeight = 50.0;
    [self.nameTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(20);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.nameTextField.mas_leading).offset(-10);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameTitleLabel);
        make.width.greaterThanOrEqualTo(@100);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [self.separator1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameTitleLabel.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(1);
    }];

    [self.idTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.separator1.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.idTextField.mas_leading).offset(-10);
        make.height.mas_equalTo(labelHeight);
    }];

    [self.idTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.idTitleLabel);
        make.width.greaterThanOrEqualTo(@100);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(labelHeight);
    }];

    [self.separator2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.idTitleLabel.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(1);
    }];

    [self.birthdayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.separator2.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.birthdayTextField.mas_leading).offset(-10);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [self.birthdayTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.birthdayLabel);
        make.width.greaterThanOrEqualTo(@100);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [self.separator3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.birthdayLabel.mas_bottom).offset(1);
        make.leading.equalTo(self.containerImageView).offset(30);
        make.trailing.equalTo(self.containerImageView).offset(-30);
        make.height.mas_equalTo(1);
    }];
}

- (UIImageView *)containerImageView {
    if (!_containerImageView) {
        _containerImageView = [[UIImageView alloc] init];
        _containerImageView.contentMode = UIViewContentModeScaleAspectFit;
        _containerImageView.image = [UIImage imageNamed:@"auth_cer_completed_bg"];
        [self.view addSubview:_containerImageView];
    }
    return _containerImageView;
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        _iconImageView.image = [UIImage imageNamed:@"auth_completed_icon"];
        [self.view addSubview:_iconImageView];
    }
    return _iconImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont boldSystemFontOfSize:15];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#8500FD"];
        _titleLabel.text = LocalizableForKey(@"Authentication successful！");
        [self.view addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UIButton *)continueButton {
    if (!_continueButton) {
        _continueButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_continueButton setBackgroundImage:[UIImage imageNamed:@"button_bg_style5"] forState: UIControlStateNormal];
        [_continueButton setTitle:LocalizableForKey(@"Continue certification") forState:UIControlStateNormal];
        [_continueButton setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
        _continueButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:400];
        [_continueButton addTarget:self action:@selector(clickContinueButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_continueButton];
    }
    return _continueButton;
}

- (UILabel *)nameTitleLabel {
    if (!_nameTitleLabel) {
        _nameTitleLabel = [[UILabel alloc] init];
        _nameTitleLabel.text = LocalizableForKey(@"Aadhaar Name");
        _nameTitleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        _nameTitleLabel.textColor = [UIColor colorWithHexString:@"#727272"];
        _nameTitleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _nameTitleLabel;
}

- (UILabel *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = [[UILabel alloc] init];
        _nameTextField.font = [UIFont systemFontOfSize:12 weight:UIFontWeightSemibold];
        _nameTextField.textColor = [UIColor colorWithHexString:@"#000000"];
        _nameTextField.textAlignment = NSTextAlignmentRight;
        _nameTextField.adjustsFontSizeToFitWidth = YES;
    }
    return _nameTextField;
}

- (UILabel *)idTitleLabel {
    if (!_idTitleLabel) {
        _idTitleLabel = [[UILabel alloc] init];
        _idTitleLabel.text = LocalizableForKey(@"Aadhaar Number");
        _idTitleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        _idTitleLabel.textColor = [UIColor colorWithHexString:@"#727272"];
        _idTitleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _idTitleLabel;
}

- (UILabel *)idTextField {
    if (!_idTextField) {
        _idTextField = [[UILabel alloc] init];
        _idTextField.font = [UIFont systemFontOfSize:12 weight:UIFontWeightSemibold];
        _idTextField.textColor = [UIColor colorWithHexString:@"#000000"];
        _idTextField.textAlignment = NSTextAlignmentRight;
        _idTextField.adjustsFontSizeToFitWidth = YES;
    }
    return _idTextField;
}

- (UILabel *)birthdayLabel {
    if (!_birthdayLabel) {
        _birthdayLabel = [[UILabel alloc] init];
        _birthdayLabel.text = LocalizableForKey(@"Birthday");
        _birthdayLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        _birthdayLabel.textColor = [UIColor colorWithHexString:@"#727272"];
        _birthdayLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _birthdayLabel;
}

- (UILabel *)birthdayTextField {
    if (!_birthdayTextField) {
        _birthdayTextField = [[UILabel alloc] init];
        _birthdayTextField.font = [UIFont systemFontOfSize:12 weight:UIFontWeightSemibold];
        _birthdayTextField.textColor = [UIColor colorWithHexString:@"#000000"];
        _birthdayTextField.textAlignment = NSTextAlignmentRight;
        _birthdayTextField.adjustsFontSizeToFitWidth = YES;
    }
    return _birthdayTextField;
}

- (UIView *)separator1 {
    if (!_separator1) {
        _separator1 = [[UIView alloc] init];
        _separator1.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    }
    return _separator1;
}

- (UIView *)separator2 {
    if (!_separator2) {
        _separator2 = [[UIView alloc] init];
        _separator2.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    }
    return _separator2;
}

- (UIView *)separator3 {
    if (!_separator3) {
        _separator3 = [[UIView alloc] init];
        _separator3.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    }
    return _separator3;
}

#pragma mark - Method
- (void)clickContinueButton {
    NSDictionary *dic = @{
        @"followUpData": self.currentOmniosity
    };
    [[NSNotificationCenter defaultCenter] postNotificationName:kFollowUpMyOrderKey
                                                        object:nil
                                                      userInfo:dic];
}

@end
